// //index.js
import store from "../../utils/factory";
import {
    ysSearch,
    getYsDetail
} from "../../utils/config";

//获取应用实例
const app = getApp();
let that = null;

Component({
    pageLifetimes: {
        show() {
            if (typeof this.getTabBar === "function" && this.getTabBar()) {
                this.getTabBar().setData({
                    selected: 0
                });
            }
            this.setData({
                list: [],
                stop: false,
                page: 1
            })
            this.ysSearch();
        }
    },
    /**
     * 组件的初始数据
     */
    data: {
        scrollTop: 0,
        page: 1,
        note: [],
        list: [],
        stop: false,
        remmond: [],
        date: null,
        dateTxt: "预产期时间",
        skillTxt: "乳房护理",
        skill: null,
        startDateTxt:"预产期时间",
        startDate:null,
        skillTxt2: "月子餐",
        skill2: null,
        typeTxt: "孕妇类型",
        type: null,
        yearTxt: "护理年数",
        year: null,
        typeArray: [{
            id: "",
            name: '不限'
        }, {
            id: "r1",
            name: '预产期'
        }, {
            id: "r2",
            name: '已生产'
        }],
        skillArray: [{
            id: "",
            name: '不限'
        }, {
            id: "1",
            name: '会乳房护理'
        }, {
            id: "0",
            name: '不会乳房护理'
        },],
        skillArray2: [{
            id: "",
            name: '不限'
        }, {
            id: "1",
            name: '会月子餐'
        }, {
            id: "0",
            name: '不会月子餐'
        },],
        yearArray: [{
            id: "",
            name: '不限'
        },
        {
            id: "1",
            name: '护理年数1年'
        },
        {
            id: "2",
            name: '护理年数2年'
        },
        {
            id: "3",
            name: '护理年数3年'
        },
        {
            id: "4",
            name: '护理年数4年'
        },
        {
            id: "5",
            name: '护理年数5年及以上'
        },
        ]
    },

    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
        that = this;
    }, // 此处attached的声明会被lifetimes字段中的声明覆盖
    ready: function () { },
    /**
     * 组件的方法列表
     */
    methods: {
        onShareAppMessage() {
            return {
                title: "月嫂服务",
                path: '/pages/index/index'
            }
        },
        /**
 * 分享到朋友圈
 * 前提是必须定义了：﻿onShareAppMessage，传参是在query中定义
 * 这个方法中分享的地址就是当前页面地址，所以不需要指定path
 */
        onShareTimeline: function (res) {
            return {
                title: '月嫂服务'
            }
        },
        bindDateChange(e) {
            this.setData({
                date: e.detail.value,
                dateTxt: `预产期时间：${e.detail.value}`
            })
            this.highSearch()
        },
        bindStartDateChange(e){
            this.setData({
                startDate: e.detail.value,
                startDateTxt:e.detail.value
            })
            this.highSearch()
        },
        bindYearChange(e) {
            this.setData({
                year: that.data.yearArray[e.detail.value].id,
                yearTxt: that.data.yearArray[e.detail.value].name
            })
            console.log("year", this.data.year)
            this.highSearch()
        },
        bindSkillChange(e) {
            this.setData({
                skill: that.data.skillArray[e.detail.value].id,
                skillTxt: that.data.skillArray[e.detail.value].name
            })
            this.highSearch()
            console.log("skill", this.data.skill)
        },
        bindSkillChange2(e) {
            this.setData({
                skill2: that.data.skillArray2[e.detail.value].id,
                skillTxt2: that.data.skillArray2[e.detail.value].name
            })
            this.highSearch()
            console.log("skill2", this.data.skill2)
        },
        bindTypeChange(e) {
            this.setData({
                type: that.data.typeArray[e.detail.value].id,
                typeTxt: that.data.typeArray[e.detail.value].name
            })
            this.highSearch()
            console.log("type", this.data.type)
        },
        getYsDetail(e) {
            const id = e.currentTarget.dataset.id;
            wx.navigateTo({
                url: `../logs/logs?id=${id}`
            });
        },
        appointment(e) {
            const obj = e.currentTarget.dataset.obj;
            console.log(obj)
            if (!store.get("userInfo")) {
                wx.navigateTo({
                    url: "../autoLogin/autoLogin" // 页面 B
                });
                return false;
            }
            store.set("ysDetail", obj)
            wx.navigateTo({
                url: `../submitOrder/submitOrder`
            });
        },
        initData() {
            that.setData({
                date: null,
                dateTxt: "预产期时间",
                skillTxt: "乳房护理",
                skill: null,
                skillTxt2: "月子餐",
                skill2: null,
                typeTxt: "产妇类型",
                type: null,
                yearTxt: "护理年数",
                year: null,
                page: 1,
                list: [],
                stop: false
            })
            this.ysSearch();
        },
        highSearch() {
            if (this.data.type == "r1" && !this.data.date) {
                wx.showModal({
                    title: "温馨提示",
                    content: "预产期孕妇请选择预产期时间",
                    showCancel: false,
                    success: function (sm) { }
                });
                return false
            }
            that.setData({
                page: 1,
                list: [],
                stop: false
            })
            this.ysSearch();
        },
        goDetail(e) {
            const id = e.currentTarget.dataset.id;
            console.log(id);
            wx.navigateTo({
                url: `../logs/logs?id=${id}`
            });
        },
        //监听屏幕滚动 判断上下滚动
        onPageScroll: function (ev) {
            this.setData({
                scrollTop: ev.scrollTop > 200 ? 1 : ev.scrollTop / 200
            });
        },
        goSearch() {
            wx.navigateTo({
                url: `../search/search`
            });
        },
        onReachBottom() {
            if (this.data.stop) {
                return false
            }
            this.setData({
                page: this.data.page + 1
            })
            this.ysSearch();
        },
        ysSearch() {
            wx.showLoading({
                title: "加载中..."
            });
            let date = ""
            if (this.data.type == "r1") {
                date = this.data.date
            }
            if (this.data.type == "r2") {
                let now = new Date();
                let year = now.getFullYear();
                let month = now.getMonth() + 1;
                date = `${year}-${month}-${now.getDate()}`
                console.log(date)
            }
            ysSearch({
                page: this.data.page,
                limit: 20,
                seniority: this.data.year ? this.data.year + 1 : "",
                breastCare: this.data.skill || "",
                moonMeal: this.data.skill2 || "",
                startDate:this.data.startDate||""
            }).then(res => {
                let req = res.data;
                if (req.length < 10) {
                    that.setData({
                        stop: true
                    })
                }
                req.map(item => {
                    item.star = parseInt(item.star);
                })
                that.setData({
                    list: [...that.data.list, ...req]
                })
                wx.hideLoading()
            });
        }
    }
});